package cn.xfdzx.android.apps.shop.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDShopDetailsActivity;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.StoreInfoPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.RoundedFormation;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.ShoppingCartAnimationView;
import cn.xfdzx.android.apps.shop.view.TagTextView;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：新发地 商铺 商品
 */
public class XfdShopGoodAdapter extends RecyclerView.Adapter<XfdShopGoodAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<HomeSearchXFDGoodBeen.DataBean> mList;
    private Activity mContext;
    private int proportion;
    private StoreInfoPresenter mPresenter;
    private int end_postion[];

    public XfdShopGoodAdapter(Activity context, List<HomeSearchXFDGoodBeen.DataBean> list,StoreInfoPresenter presenter,int postion[]) {
        this.mList = list;
        this.mContext = context;
        this.mPresenter=presenter;
        this.end_postion=postion;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_xfd_shop_goods, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        if (proportion!=0){
            proportion=holder.ivFineSelect.getWidth()/holder.ivFineSelect.getHeight();
        }
        //顶部左边圆角
        RoundedFormation transformation = new RoundedFormation
                (10, 0, RoundedFormation.CornerType.TOP_LEFT);
        //顶部右边圆角
        RoundedFormation transformation1 = new RoundedFormation
                (10, 0, RoundedFormation.CornerType.TOP_RIGHT);
        //组合各种Transformation,
        MultiTransformation<Bitmap> mation = new MultiTransformation<>
                //Glide设置圆角图片后设置ImageVIew的scanType="centerCrop"无效解决办法,将new CenterCrop()添加至此
                (new CenterCrop(), transformation, transformation1);
        RequestOptions options = RequestOptions.bitmapTransform(mation).override(200*proportion, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback( R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(mation)).into(holder.ivFineSelect);
        holder.tvCarNum.setText(mList.get(position).getAdd_cart_num() + "");
        if (mList.get(position).getAdd_cart_num() == 0) {
            holder.tvCarNum.setVisibility(View.GONE);
        } else {
            holder.tvCarNum.setVisibility(View.VISIBLE);
        }
        if (null != mList.get(position).getUnit()) {
            if (mList.get(position).getUnit().isEmpty()) {
                if (Double.parseDouble(mList.get(position).getDiscountprice()) == 0) {
                    holder.tvPriceBox.setText("¥"+mList.get(position).getMarketprice());
                } else {
                    holder.tvPriceBox.setText("¥"+mList.get(position).getDiscountprice());
                }
            } else {
                if (Double.parseDouble(mList.get(position).getDiscountprice()) == 0) {
                    if (mList.get(position).getIs_self_store()==1){
                        holder.tvPriceBox.setText("¥" + mList.get(position).getMarketprice() + "/" + "斤");
                    }else {
                        holder.tvPriceBox.setText("¥" + mList.get(position).getMarketprice() + "/" + mList.get(position).getUnit());
                    }
                } else {
                    if (mList.get(position).getIs_self_store()==1){
                        holder.tvPriceBox.setText("¥" + mList.get(position).getDiscountprice() + "/" + "斤");
                    }else {
                        holder.tvPriceBox.setText("¥" + mList.get(position).getDiscountprice() + "/" + mList.get(position).getUnit());
                    }
                }

            }
            if (mList.get(position).getIs_self_store()==1){
                holder.tv_ziying.setVisibility(View.VISIBLE);
                holder.ttvContent.setSingleTagAndContent("", mList.get(position).getTitle());

            }else {
                holder.tv_ziying.setVisibility(View.GONE);
                if (null == mList.get(position).getGoods_ensures()) {
                    holder.ttvContent.setText(mList.get(position).getTitle());
                } else {

                    holder.ttvContent.setSingleTagAndContent("", mList.get(position).getTitle());
                }
            }

            holder.tvSaleNum.setText("已售" + mList.get(position).getSale_num() + "笔");
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClick()) {
                        mItemClickListener.onItemClick(position);
                    }
                }
            });
        }

        if (mList.get(position).getIs_close() == 1) {
            //已售罄
            holder.ivCar.setImageResource(R.drawable.icon_home_pifa_che_hui);
            holder.ivCar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        } else {
            holder.ivCar.setImageResource(R.drawable.icon_home_pifa_che_lv);
            holder.ivCar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (EmptyUtil.isEmpty(APP_TOKEN)){
                        Intent intent = new Intent(mContext, OneLoginMainActivity.class);
                        mContext.startActivity(intent);
                        return;
                    }
                    ShoppingCartAnimationView shoppingCartAnimationView = new ShoppingCartAnimationView(mContext);
                    shoppingCartAnimationView.setPiFaColor();
                    int postion[] = new int[2];
                    holder.ivCar.getLocationInWindow(postion);
                    shoppingCartAnimationView.setStartPosition(new Point(postion[0], postion[1]));
                    ViewGroup rootView = (ViewGroup)mContext. getWindow().getDecorView();
                    rootView.addView(shoppingCartAnimationView);
                    shoppingCartAnimationView.setEndPosition(new Point( end_postion[0], end_postion[1]));
                    shoppingCartAnimationView.startBeizerAnimation();
//                    mPresenter.addCar("0","", mList.get(position).getId()+"","1");
                    if (mList.get(position).getIs_self_store()==1) {//自营
                        if (mList.get(position).getAdd_cart_num() >= mList.get(position).getIs_support()) {
                            mPresenter.addCar("0", "", mList.get(position).getId() + "", "1", position,mList.get(position).getAdd_cart_num()+1);
                        } else {
                            mPresenter.addCar("0", "", mList.get(position).getId() + "",mList.get(position).getIs_support()+"", position, mList.get(position).getAdd_cart_num()+mList.get(position).getIs_support());
                        }
                    } else {
                        mPresenter.addCar("0", "", mList.get(position).getId() + "", "1", position,mList.get(position).getAdd_cart_num()+1);
                    }
                 }
            });
        }
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_fine_select)
        ImageView ivFineSelect;
        @BindView(R.id.tv_content)
        TagTextView ttvContent;
        @BindView(R.id.tv_price_box)
        TextView tvPriceBox;
        @BindView(R.id.tv_sale_num)
        TextView tvSaleNum;
       @BindView(R.id.tv_car_num)
        TextView tvCarNum;
        @BindView(R.id.iv_car)
        ImageView ivCar;
        @BindView(R.id.tv_ziying)
        TextView tv_ziying;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
